﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using Spore.Validator;

namespace Client
{
    public partial class FormVipDeferReq : BasePopForm
    {
        List<QunYing.Model.VipServices> services;
        List<QunYing.Model.VipRank> vipRank;
        QunYing.BLL.VipPayOrder bll_VipPayOrder= new QunYing.BLL.VipPayOrder();
      
        public QunYing.Model.VipPayOrder VipPayOrder
        {
            get;
            private set;
        }

        private QunYing.Model.VipInfo vipInfo;

        public FormVipDeferReq()
        {
            InitializeComponent();
            this.services = (new QunYing.BLL.VipServices()).GetModelList(string.Empty);
            this.vipRank = (new QunYing.BLL.VipRank()).GetModelList(string.Empty);
            this.cbe_会员级别.SelectedIndexChanged += new EventHandler(cbe_会员级别_SelectedIndexChanged);
            this.FormValidator.SetControlValitors(this.cbe_会员级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.det_到期时间, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_缴费金额, ValidatorDefaults.Integer);
            this.NecessaryRemark(this.cbe_会员级别, this.det_到期时间, this.txt_缴费金额, this.txt_服务项);

        }

        void cbe_会员级别_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_服务项.BeginUpdate();
            this.txt_服务项.Items.Clear();
            string[] strs = vipRank.First((v) => { return v.等级名称 == cbe_会员级别.Text; }).基本服务条款.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (var v in strs)
            {
                var temp = services.First((vv) => { return vv.ID.ToString() == v; });
                this.txt_服务项.Items.Add(temp, true);
            }
            this.txt_服务项.EndUpdate();
        }

        public void BindData(QunYing.Model.VipInfo vipInfo)
        {
            this.vipInfo = vipInfo;
            this.VipPayOrder = new QunYing.Model.VipPayOrder();
            this.VipPayOrder.会员卡号 = vipInfo.会员卡号;
            this.VipPayOrder.经销商ID = vipInfo.经销商编号;
            this.VipPayOrder.原到期时间 = vipInfo.到期时间.ToString();
            this.VipPayOrder.原服务选项 = vipInfo.所选服务项;
            this.VipPayOrder.审核状态 = "未审核";
            this.VipPayOrder.会员原级别 = vipInfo.会员级别;
            this.lbl_会员卡号.Text = this.vipInfo.会员卡号;
            this.lbl_经销商.Text = this.vipInfo.经销商编号;
            this.lbl_到期时间.Text = this.vipInfo.到期时间.ToString("yyyy-MM-dd");
            this.lbl_级别.Text = this.vipInfo.会员级别.ToString();
            foreach (var v in this.cbe_会员级别.Properties.Items)
            {
                if (v.ToString() == vipInfo.会员级别)
                {
                    cbe_会员级别.SelectedItem = v;
                    break;
                }
            }
        }

        private void sharpButton1_Click(object sender, EventArgs e)
        {
            if(!this.FormValidator.ValidAll())  return;
            string services = string.Empty;
            foreach (var vv in this.txt_服务项.CheckedItems)
            {
                services += this.services.FirstOrDefault((ee) => ee.服务项 == vv.ToString()).ID.ToString() + ",";
            }
            services = services.TrimEnd(',');
            this.VipPayOrder.新到期时间 = this.det_到期时间.DateTime;
            this.VipPayOrder.缴费金额 = Int32.Parse(this.txt_缴费金额.Text.Trim());
            this.VipPayOrder.会员新级别 = this.cbe_会员级别.Text;
            this.VipPayOrder.提交时间 = DateTime.Now;
            this.VipPayOrder.新服务选项 = services;
            this.bll_VipPayOrder.Add(VipPayOrder);
            GlobalInfo.MsgBoxShowInfo("续费信息已提交，正在审核.");
            this.DialogResult = DialogResult.OK;
        }

        private void sharpButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }
        
    }
}